package app.conn;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import app.Main;

/**
 * 
 * 
 * @author Lukas Vavra
 *
 */
public class GreenhouseConnector {
	private static final String OK = "<ok>";
	private static final String FAIL = "<fail>";
	private static final String USR_EXIST = "<usrexist>";

	private static HttpConnector http;

	/**
	 *  Create instance of HttpConnector for perform communication with Greenhouse server
	 *  
	 * @throws IOException Connection failed, server isn't reachable
	 */
	public static void init() throws IOException {
		if(http == null)
			http = new HttpConnector(Main.URL);
	}


	/**
	 * Login to server interface
	 * 
	 * @param user User name
	 * @param pass User password
	 * @return Generated token for next communication if logged in, else return null
	 * @throws IOException Connection error, server is not responding
	 */
	public static String login(String user, String pass) throws IOException {
		init();
		String[] param = new String[3];

		try {
			param[0] = "user=".concat(user);
			param[1] = "pass=".concat(HttpConnector.md5(pass));
			param[2] = "action=login";

			http.send(param);

			String[] result = http.receive();
			if(result != null && result.length > 0 && result[0].equals(OK)) {
				return result[1];
			}
		} catch (NoSuchAlgorithmException e) {
			Main.showError("Chyba algoritmu", e.toString());
			System.exit(-1);
		}


		return null;
	}

	/**
	 * Register new user
	 * 
	 * @param user User name
	 * @param pass User password (in naked string format)
	 * @return Generated token for next communication if logged in, else return null
	 * @throws IOException Connection error, server is not responding
	 */
	public static String register(String user, String pass) throws IOException {
		init();
		String[] param = new String[3];

		try {
			param[0] = "user=".concat(user);
			param[1] = "pass=".concat(HttpConnector.md5(pass));
			param[2] = "action=register";

			http.send(param);

			String[] result = http.receive();
			if(result != null && result.length > 0) {
				if(result[0].equals(OK)) {
					return result[1];
				}
				else if(result[0].equals(USR_EXIST)) {
					Main.showError("Chybné uživatelské jméno", "Uživatel již existuje");
					return null;
				}
				else if(result[0].equals(FAIL)) {
					Main.showError("Chyba serveru", result[1]);
					return null;
				}
				
			}
		} catch (NoSuchAlgorithmException e) {
			Main.showError("Chyba algoritmu", e.toString());
			return null;
		}
		return null;
	}
	
	/**
	 * Get list of all connected units with unit name and unit id 
	 * 
	 * @return unitid - unitname pairs stored in Map
	 * @throws IOException Connection error, server is not reachable
	 */
	public static Map<Integer, String> getUnitList() throws IOException {
		init();
		/* check that user is logged in */
		if(Main.token == null) {
			Main.showError("Chyba přihlášení", "Nepřihlášený uživatel");
			return null;
		}
		
		/* prepare request */
		String[] param = new String[2];
		param[0] = "token=" + Main.token;
		param[1] = "action=unitlist";
		
		/* send a request and get a response */
		http.send(param);
		String[] result = http.receive();
		
		/* verify received data */
		if(result != null && result.length > 1 && result[0].equals(OK)) {
			/* parse response to Map */
			Map<Integer, String> map = new HashMap<Integer, String>();
			for(int i = 1; i < result.length; i++) {
				String[] splited = result[i].split(" ", 2);
				map.put(Integer.parseInt(splited[0]), splited[1]);
			}
			return map;
		}		
		
		return null;
	}
	
	public static boolean addUnit(String name) throws IOException {
		init();
		
		
		
		return false;
	}


}
